//
//  HQNotiViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/5.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQNotiViewController.h"

@interface HQNotiViewController ()

@end

@implementation HQNotiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"消息中心";
    [self requestUserContent];
    [self addGesture];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HQNotiCell *cell = [HQNotiCell cellWithTableView:tableView];
    cell.dataInfo = _dataSource[indexPath.row];
    [cell.acceptItem addTarget:self action:@selector(acceptItem:) forControlEvents:UIControlEventTouchUpInside];
    [cell.rejectItem addTarget:self action:@selector(rejectItem:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // 消息类型为2的接口不能修改状态，只能通过操作接收或拒绝按钮来修改
    NSString *msgtype = _dataSource[indexPath.row][@"msgtype"];
    if([msgtype integerValue] == 2){
        return;
    }
    NSString *state = _dataSource[indexPath.row][@"state"];
    if([state integerValue] == 2){
        [self showHudText:@""];
        [HQNetWork requetParam:[HQParam userIsState:_dataSource[indexPath.row][@"id"]] withMethod:[HQMethod userIsState] suc:^(NSDictionary *sucData) {
            [self hiddnHud];
            if([sucData[kStatus]integerValue] == 1){
                [self requestUserContent];
            }else{
                [self tostView:sucData[kInfo]];
            }
        } fail:^{
            [self hiddenErrorHud];
        }];
    }
}

// 删除消息
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    [self deleteMessage:_dataSource[indexPath.row][@"id"]];
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

/**
 删除消息接口
 
 @param msgId 消息ID
 */
-(void)deleteMessage:(NSString *)msgId{
    [self showHudText:@"删除中..."];
    [HQNetWork requetParam:[HQParam delMessage:[UserModelManager userID] withMessageId:msgId] withMethod:[HQMethod userDelCom] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            [self tostView:@"消息删除成功"];
            [self requestUserContent];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 请求消息列表
 */
-(void)requestUserContent{
    [self showHudText:@""];
    _tableView.backgroundView = nil;
    [HQNetWork requetParam:[HQParam userContent:[UserModelManager userID]] withMethod:[HQMethod userContent] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            _dataSource = [[NSMutableArray alloc]initWithArray:sucData[@"data"]];
            if(_dataSource.count == 0){
                [self addNoneDataView:_tableView withText:@"列表内容为空"];
            }
            [_tableView reloadData];
        }else{
            if(_dataSource.count == 0){
                [self addNoneDataView:_tableView withText:@"列表内容为空"];
            }
            if(![sucData[kInfo] isEqualToString:@"暂无数据"]){
                [self tostView:sucData[kInfo]];
            }
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 接受合作
 
 @param button 按钮
 */
-(void)acceptItem:(HQButton *)button{
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam acceptshop:[UserModelManager userID] _id:button.dataInfo[@"id"]] withMethod:[HQMethod acceptshop] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您已经接受了合作邀请！" preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                [self requestUserContent];
            }]];
            [self presentViewController:alertController animated:YES completion:nil];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 拒绝合作
 
 @param button 按钮
 */
-(void)rejectItem:(HQButton *)button{
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam danyshop:[UserModelManager userID] _id:button.dataInfo[@"id"]] withMethod:[HQMethod danyshop] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您已经拒绝了合作邀请！" preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                [self requestUserContent];
            }]];
            [self presentViewController:alertController animated:YES completion:nil];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

-(void)addGesture{
    
}

@end
